package com.yikome.shop.utils;

import java.math.BigDecimal;
import java.util.List;

import com.yikome.shop.api.dto.CollectionApiDTO;
import com.yikome.shop.api.dto.ProductApiDTO;
import com.yikome.shop.api.dto.ProductSkuApiDTO;

public class ExchangeRateUtils {

	public static List<CollectionApiDTO> filterCollectionApiDTO(List<CollectionApiDTO> collectionApiDTOs , BigDecimal exchangeRate) {
		
		if(collectionApiDTOs == null || collectionApiDTOs.isEmpty()) return collectionApiDTOs ;
		
		for (CollectionApiDTO collectionApiDTO : collectionApiDTOs) {
			
			List<ProductApiDTO> productDTOs = collectionApiDTO.getProductDTOs();
			
			if(productDTOs != null && !productDTOs.isEmpty()) {
				
				for (ProductApiDTO productApiDTO : productDTOs) {
					
					productApiDTO.setPrice(productApiDTO.getPrice().multiply(exchangeRate).setScale(0, BigDecimal.ROUND_DOWN));
					productApiDTO.setOriginalPrice(productApiDTO.getOriginalPrice().multiply(exchangeRate).setScale(0, BigDecimal.ROUND_DOWN));
				}
			}
		}
		return collectionApiDTOs ; 
		
	}
	
	public static ProductApiDTO filterProductApiDTO(ProductApiDTO productApiDTO , BigDecimal exchangeRate) {
		
		productApiDTO.setPrice(productApiDTO.getPrice().multiply(exchangeRate).setScale(0, BigDecimal.ROUND_DOWN));
		productApiDTO.setOriginalPrice(productApiDTO.getOriginalPrice().multiply(exchangeRate).setScale(0, BigDecimal.ROUND_DOWN));
		
		List<ProductApiDTO> similarProducts = productApiDTO.getSimilarProduct();
		if(similarProducts != null && !similarProducts.isEmpty()) {
			for (ProductApiDTO similarProduct : similarProducts) {
				similarProduct.setPrice(similarProduct.getPrice().multiply(exchangeRate).setScale(0, BigDecimal.ROUND_DOWN));
				similarProduct.setOriginalPrice(similarProduct.getOriginalPrice().multiply(exchangeRate).setScale(0, BigDecimal.ROUND_DOWN));
			}
		}
		
		List<ProductApiDTO> likeProducts = productApiDTO.getLikeProduct();
		if(likeProducts != null && !likeProducts.isEmpty()) {
			for (ProductApiDTO likeProduct : likeProducts) {
				likeProduct.setPrice(likeProduct.getPrice().multiply(exchangeRate).setScale(0, BigDecimal.ROUND_DOWN));
				likeProduct.setOriginalPrice(likeProduct.getOriginalPrice().multiply(exchangeRate).setScale(0, BigDecimal.ROUND_DOWN));
			}
		}
		
		List<ProductSkuApiDTO> productSkuApiDTOs = productApiDTO.getProductSkuApiDTOs();
		for (ProductSkuApiDTO productSkuApiDTO : productSkuApiDTOs) {
			productSkuApiDTO.setPrice(productSkuApiDTO.getPrice().multiply(exchangeRate).setScale(0, BigDecimal.ROUND_DOWN));
		}
		
		return productApiDTO ;
	}
	
	public static BigDecimal filterPrice(BigDecimal price , BigDecimal exchangeRate) {
		
		return price.multiply(exchangeRate).setScale(0, BigDecimal.ROUND_DOWN);
	}

	public static List<ProductApiDTO> filterRecomendHosts(List<ProductApiDTO> recomendHosts, BigDecimal exchangeRate) {
		
		for (ProductApiDTO productApiDTO : recomendHosts) {
			productApiDTO.setPrice(productApiDTO.getPrice().multiply(exchangeRate).setScale(0, BigDecimal.ROUND_DOWN));
			productApiDTO.setOriginalPrice(productApiDTO.getOriginalPrice().multiply(exchangeRate).setScale(0, BigDecimal.ROUND_DOWN));
		}
		return recomendHosts;
	}
}
